#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

struct element
{
int key;
char uname[256], pass[256];
struct element *forward;
};

struct element data[6];
struct element *key_table[6];

int key_create(char *p)
{
int k = -1;
int i;
for (i=0;i<5;i++)
{
	if (strcmp(p, data[i].uname) == 0)
	{
	k = i;
	break;
	}
}
return k;
}

void fill_data()
{
FILE *fopen(), *fp;
fp = fopen("data.txt","r");
int i;
for (i=0;i<6;i++)
{
	fscanf(fp, "%s", data[i].uname);
	fscanf(fp, "%s", data[i].pass);
	data[i].key = key_create(data[i].uname);
	key_table[i] = &data[i];
}
}

void main()
{
int i, count, u_key, flag, exit_key;
char u[256], p[256];
flag = 0;
count = 0;
exit_key = 0;
struct element *comp;
for (i=0;i<5;i++)
{
key_table[i] = NULL;
}
fill_data();
while (count != 3 && exit_key ==0)
{
	printf("enter username: ");
	scanf("%s", u);
	u_key = key_create(u);
	comp = key_table[u_key];
	printf("enter password: ");
	scanf("%s", p);
	if (u_key != -1)
	{
		if ((strcmp((comp->pass), p))==0)
		{
			printf("correct username and password\n");
			printf("enter 0 to continue and 1 to exit: ");
			scanf("%d", &exit_key);
			continue;
		}
		else
		{
			printf("incorrect password. \n");
			count++;
		}
	}
	else
	{
		printf("incorrect username. \n");
		count++;
	}
}
if (count ==3)
{
	printf("you have entered incorrect username or password 3 times. program will now terminate. \n");
}
}
